package com.ljc.service;

import com.ljc.dao.ExpertDao;
import com.ljc.entity.Administrator;
import com.ljc.entity.Expert;
import com.ljc.entity.ExpertGradeCount;
import com.ljc.vo.ExpertSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class ExpertServiceImpl  implements ExpertService{
    @Autowired
    private ExpertDao expertDao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Expert> searchExpert(ExpertSearch expertSearch) {
        return expertDao.searchExpert(expertSearch);
    }

    @Override
    public List<ExpertGradeCount> getExpertGradeCount(int deanId) {
        return expertDao.getExpertGradeCount(deanId);
    }

    @Override
    public void addExpert(Expert expert) {
        Expert expertDB = expertDao.getOne(expert.getName());
        if (expertDB!=null){
            throw new  RuntimeException("该用户名已被注册");
        }else{
            expertDao.addOne(expert);
        }
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Expert findExpertById(int id) {
        Expert expert = expertDao.findExpertById(id);
        return expert;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateExpert(Expert expert) {
        expertDao.updateExpert(expert);
    }

    @Override
    public void deleteExpert(int id) {
        expertDao.deleteExpert(id);
    }

    @Override
    public List<Expert> getActiveExpert(int deanId) {
        return expertDao.getActiveExpert(deanId);
    }

    @Override
    public Expert login(Expert expert) {
        Expert expertDB = expertDao.getOne(expert.getName());
        if(expertDB!=null){
            if(!expertDB.getPassword().equals(expert.getPassword())){
                throw new RuntimeException("密码错误");
            }else if (expertDB.getStatus()==0){
                throw new RuntimeException("该用户已经被管理员禁用");
            } else if (expertDB.getDeleted()==1){
                throw new RuntimeException("该用户已经被管理员删除");
            }else{
                return expertDB;
            }
        }else{
            throw new RuntimeException("该用户名不存在");
        }
    }
}
